﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;

namespace UtfprLibrary.Windows.Forms.ComponentModel {
    /// <summary>
    /// TODO: Documentar
    /// </summary>
    public class UtfprSourceNavigatorConverter : ReferenceConverter {
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="type"></param>
        public UtfprSourceNavigatorConverter(Type type) : base(type) { }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="context"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected override bool IsValueAllowed(ITypeDescriptorContext context, object value) {
            try {
                //linkedControl
                var navigator = context.Instance.GetType().GetField("LinkedControl", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(context.Instance) as UtfprBindingNavigatorBase;
                bool isMaster = navigator.CurrentMode == UBNModeEnum.Master;
                bool isCurrentReference = navigator == value;
                return (!isMaster && !isCurrentReference);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
    }
}
